package com.zzj.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzj.base.Result;
import com.zzj.service.*;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedisService redisService;
    @Resource
    private INoticeService noticeService;


    @PostMapping("getNoRead")
    public Result getNoRead(@RequestHeader(value = "Authorization") String token) {
        String username = JWTUtil.getUsername(token);

        int noticeNoReadNum = noticeService.getNoticeNoReadNum(username);
        int commentNoReadNum = 0;
        int replyNoReadNum = 0;
        int likeNoReadNum = 0;


        String commentNoReadId = redisService.getCommentNoReadId(username);
        if (commentNoReadId != null && !commentNoReadId.equals("")) {
            commentNoReadNum = commentNoReadId.split(",").length;
        }
        String replyNoReadId = redisService.getReplyNoReadId(username);
        if (replyNoReadId != null && replyNoReadId != "") {
            replyNoReadNum = replyNoReadId.split(",").length;
        }
        String likeNoReadId = redisService.getLikeNoReadId(username);
        if (likeNoReadId != null && likeNoReadId != "") {
            likeNoReadNum = likeNoReadId.split(",").length;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("noticeNoReadNum", noticeNoReadNum);
        jsonObject.put("commentNoReadNum", commentNoReadNum);
        jsonObject.put("replyNoReadNum", replyNoReadNum);
        jsonObject.put("likeNoReadNum", likeNoReadNum);
        return Result.success(jsonObject);
    }
}
